package controller;

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.Animal;

public class AnimalSettings extends JPanel{
	
	private JSlider age;
	private JSlider breedAge;
	private JSlider breedProb;
	private JSlider litterSize;
	
	private JLabel name;
	private JLabel imageLabel;
	private JLabel ageLabel;
	private JLabel breedAgeLabel;
	private JLabel breedProbLabel;
	private JLabel litterSizeLabel;
	
	public AnimalSettings(final Animal animal, ImageIcon image) {
		super();
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		name = new JLabel(animal.toString());
		imageLabel = new JLabel(image);
		
		ageLabel = new JLabel("Age");
		age = new JSlider(0, 200, animal.getMaxAge());
		age.setMajorTickSpacing(50);
		age.setMinorTickSpacing(10);
		age.setPaintTicks(true);
		age.setPaintLabels(true);
		age.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				animal.setMaxAge(source.getValue());
			}
		});

		breedAgeLabel = new JLabel("Breeding age");
		breedAge = new JSlider(0, 200, animal.getBreedingAge());
		breedAge.setMajorTickSpacing(50);
		breedAge.setMinorTickSpacing(10);
		breedAge.setPaintTicks(true);
		breedAge.setPaintLabels(true);
		breedAge.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				animal.setBreedingAge(source.getValue());
			}
		});

		breedProbLabel = new JLabel("Breeding probability");
		breedProb = new JSlider(0, 100, (int) (animal.getBreedingProbability()*100));
		breedProb.setMajorTickSpacing(50);
		breedProb.setMinorTickSpacing(10);
		breedProb.setPaintTicks(true);
		breedProb.setPaintLabels(true);
		breedProb.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				animal.setBreedingProbability(((double)source.getValue())/100);
			}
		});

		litterSizeLabel = new JLabel("Litter size");
		litterSize = new JSlider(0, 4, animal.getMaxLitterSize());
		litterSize.setMajorTickSpacing(2);
		litterSize.setMinorTickSpacing(1);
		litterSize.setPaintTicks(true);
		litterSize.setPaintLabels(true);
		litterSize.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				animal.setMaxLitterSize(source.getValue());
			}
		});
		
		add(name);
		add(imageLabel);
		add(ageLabel);
		add(age);
		add(breedAgeLabel);
		add(breedAge);
		add(breedProbLabel);
		add(breedProb);
		add(litterSizeLabel);
		add(litterSize);
		
		setMaximumSize(new Dimension(200, 800));
		setVisible(true);
	}

}
